/** 
 * required   必填任意字符
 * not-required-n  非必填数字
 * required-n  必填数字
 * required-c  必填[A-Za-z]
 * required-c-n 必填[0-9A-Za-z]
 * required-zh  必填中文
 * required-zh-c-n 必填[中文0-9A-Za-z]
 * 验证框架 **/

var errClass='field_err';
var tipClass='field_tip';


$(function(){
	$('input[vtype=required]').bind('focus',Fun.requiredFocus);
	$('input[vtype=required]').bind('blur',Fun.requiredBlur);
	$('input[vtype=not-required-n]').bind('focus',Fun.not_required_nFocus);
	$('input[vtype=not-required-n]').bind('blur',Fun.not_required_nBlur);
	$('input[vtype=not-required-n]').bind('keyup',Fun.onlyNumber);
	$('input[vtype=not-required-n]').bind('paste',Fun.onlyNumber);
	$('input[vtype=not-required-n]').css("ime-mode", "disabled");
	
	
	$('input[vtype=required-email]').bind('focus',Fun.requiredFocus);
	$('input[vtype=required-email]').bind('blur',Fun.requiredEmailBlur);
	
	$('input[vtype=required-letter-digital]').bind('focus',Fun.requiredFocus);
	$('input[vtype=required-letter-digital]').bind('blur',Fun.requiredLetterDigitalBlur);
	
	
	
	
	$('input[vtype=required-price]').bind('focus',Fun.requiredFocus);
	$('input[vtype=required-price]').bind('blur',Fun.requiredPriceBlur);
	$('input[vtype=not-required-price]').bind('focus',Fun.requiredFocus);
	$('input[vtype=not-required-price]').bind('blur',Fun.notRequiredPriceBlur);
	
	
	
	
});


function checkForm(){
	var isSuccess = true;

	//验证必填字段
	var objs = $('input[vtype=required]');
	if(objs[0]){
		for(var i=0;i<objs.length;i++){
			if(!Fun.requiredCheck(objs[i])){isSuccess = false;}
		}
	}
	//
	var objs = $('input[vtype=not_required_nCheck]');
	if(objs[0]){
		for(var i=0;i<objs.length;i++){
			if(!Fun.not_required_nCheck(objs[i])){isSuccess = false;}
		}
	}
	
	var objs = $('input[vtype=required-email]');
	if(objs[0]){
		for(var i=0;i<objs.length;i++){
			if(!Fun.requiredEmailCheck(objs[i])){isSuccess = false;}
		}
	}
	
	var objs = $('input[vtype=required-letter-digital]');
	if(objs[0]){
		for(var i=0;i<objs.length;i++){
			if(!Fun.requiredLetterDigitalCheck(objs[i])){isSuccess = false;}
		}
	}
	
	var objs = $('input[vtype=required-price]');
	if(objs[0]){
		for(var i=0;i<objs.length;i++){
			if(!Fun.requiredPriceCheck(objs[i])){isSuccess = false;}
		}
	}
	
	var objs = $('input[vtype=not-required-price]');
	if(objs[0]){
		for(var i=0;i<objs.length;i++){
			if(!Fun.notRequiredPriceCheck(objs[i])){isSuccess = false;}
		}
	}
	
	
	
	
	
	return isSuccess;
}




var Fun = {
		requiredFocus:function(){
			var name = $(this).attr('vname');
			$('#'+name+'_m').html('');
			var tip = Message[name+'_tip'];
			$(this).next('span').attr('class',tipClass);
			$('#'+name+'_m').html(tip);
		},
		jsFocus:function(obj){
			var name = $(obj).attr('vname');
			$('#'+name+'_m').html('');
			var tip = Message[name+'_tip'];
			$(obj).next('span').attr('class',tipClass);
			$('#'+name+'_m').html(tip);
		},
		requiredBlur:function(){
			Fun.requiredCheck(this);
		},
		requiredCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			
			var val = $.trim($(obj).val());
			if(val != ''){
				$(obj).next('span').removeAttr('class');
				$('#'+name+'_m').html('');
				return true;
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		requiredEmailBlur:function(){
			Fun.requiredEmailCheck(this);
		},
		requiredEmailCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				 var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/g;
				 if(!reg.test(val)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					$(obj).next('span').removeAttr('class');
					$('#'+name+'_m').html('');
					return true;
				}
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		requiredLetterDigitalBlur:function(){
			Fun.requiredLetterDigitalCheck(this);
		},
		requiredLetterDigitalCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				var min = parseInt($(obj).attr('vmin'));
				var max = parseInt($(obj).attr('vmax'));
				if(val.length < min || (max!=0&&max<val.length)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					var reg = /^[0-9a-zA-Z]*$/g;
					if(!reg.test(val)){
						$(obj).next('span').attr('class',errClass);
						$('#'+name+'_m').html(err);
						return false;
					}else{
						$(obj).next('span').removeAttr('class');
						$('#'+name+'_m').html('');
						return true;
					}
				}
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		requiredLetterDigitalChineseCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				var min = parseInt($(obj).attr('vmin'));
				var max = parseInt($(obj).attr('vmax'));
				if(val.length < min || (max!=0&&max<val.length)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					var reg = /^[0-9a-zA-Z\u4e00-\u9fa5]*$/g;
					if(!reg.test(val)){
						$(obj).next('span').attr('class',errClass);
						$('#'+name+'_m').html(err);
						return false;
					}else{
						$(obj).next('span').removeAttr('class');
						$('#'+name+'_m').html('');
						return true;
					}
				}
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		requiredwCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				var min = parseInt($(obj).attr('vmin'));
				var max = parseInt($(obj).attr('vmax'));
				if(val.length < min || (max!=0&&max<val.length)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					var reg = /^\w*$/g;
					if(!reg.test(val)){
						$(obj).next('span').attr('class',errClass);
						$('#'+name+'_m').html(err);
						return false;
					}else{
						$(obj).next('span').removeAttr('class');
						$('#'+name+'_m').html('');
						return true;
					}
				}
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		onlyNumber:function(){
			 $(this).val($(this).val().replace(/\D|^0/g,'')); 
		},
		not_required_nFocus:function(){
			var name = $(this).attr('vname');
			var tip = Message[name+'_tip'];
			$(this).next('span').attr('class',tipClass);
			$('#'+name+'_m').html(tip);
		},
		not_required_nBlur:function(){
			Fun.not_required_nCheck(this);
		},
		not_required_nCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val == ''){
				$(obj).next('span').removeAttr('class');
				$('#'+name+'_m').html('');
				return true;
				
			}else{
				var aint=parseInt(val);	
				if(aint>0&& (aint+'')==val){
					$(obj).next('span').removeAttr('class');
					$('#'+name+'_m').html('');
					return true;
				}else{
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}
			}
		},
		requiredPriceBlur:function(){
			Fun.requiredPriceCheck(this);
		},
		requiredPriceCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				 var reg = /^0$|^0\.\d{1,2}$|^[1-9]\d{0,9}$|^[1-9]\d{0,9}\.\d{1,2}?$/;
				 if(!reg.test(val)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					$(obj).next('span').removeAttr('class');
					$('#'+name+'_m').html('');
					return true;
				}
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		notRequiredPriceBlur:function(){
			Fun.notRequiredPriceCheck(this);
		},
		notRequiredPriceCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				 var reg = /^0$|^0\.\d{1,2}$|^[1-9]\d{0,9}$|^[1-9]\d{0,9}\.\d{1,2}?$/;
				 if(!reg.test(val)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					$(obj).next('span').removeAttr('class');
					$('#'+name+'_m').html('');
					return true;
				}
			}else{
				$(obj).next('span').removeAttr('class');
				$('#'+name+'_m').html('');
				return true;
			}
		},
		zIntCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			if(val != ''){
				 var reg = /^(0|[1-9][0-9]*)$/;
				 if(!reg.test(val)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					$(obj).next('span').removeAttr('class');
					$('#'+name+'_m').html('');
					return true;
				}
			}else{
				$(obj).next('span').attr('class',errClass);
				$('#'+name+'_m').html(err);
				return false;
			}
		},
		isNumChar:function(obj){
			var val = $.trim($(obj).val());
			if(val != ''){
				 var reg = /^[0-9a-zA-Z]+$/;
				 if(!reg.test(val)){
					return false;
				}else{
					return true;
				}
			}else{
				return false;
			}
		},
		isEmpty:function(obj){
			var val = $.trim($(obj).val());
			if(val===''){
				return false;
			}else{
				return true;
			}
		},
		urlCheck:function(obj){
			var name = $(obj).attr('vname');
			var err = Message[name+'_err'];
			var val = $.trim($(obj).val());
			var min = parseInt($(obj).attr('vmin'));
			var max = parseInt($(obj).attr('vmax'));
			if(val != ''){
				if(val.length < min || (max!=0&&max<val.length)){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}else{
					 var strRegex = "^((https|http)://)" 
						  + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184  
						  + "|"
						  + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.  
						  + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名  
						  + "[a-z]{2,6})" 
						  + "(:[0-9]{1,4})?" // 端口
						  + "((/?)|" 
						  + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";  
				    var reg=new RegExp(strRegex); 
					if(!reg.test(val)){
						$(obj).next('span').attr('class',errClass);
						$('#'+name+'_m').html(err);
						return false;
					}else{
						$(obj).next('span').removeAttr('class');
						$('#'+name+'_m').html('');
						return true;
					}
				}
			}else{
				if(min > 0){
					$(obj).next('span').attr('class',errClass);
					$('#'+name+'_m').html(err);
					return false;
				}
			}
		},
		
};





